#include "herobank.h"
#include "ui_herobank.h"
#include "studentstore.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <sstream>
#include "utils.h"
using namespace studentstore;
using namespace utils;


void herobank::show()
{
    auto size = sizeof (HeroRank);//学生类的大小
   // auto stusize=sizeof (Student);
    FILE *file = openfile(heroRankDatPath, size); //打开文件
    //FILE *stufile = openfile(studentDatPath, stusize); //打开文件
    fseek(file, 0, SEEK_END);//fseek将文件指针指到文件末尾
    auto offset = ftell(file);//ftell得到文件从指针到文件头的大小（offset是文件总大小）

    int row = offset / size; //采取和增加用户时算ID大小一样的方法算数据行数
    ui->tableWidget->setColumnCount(5); //设置列数
    QStringList header;
    header << "ID" << "学工号" << "姓名" << "专业" << "成就达成时间";
    ui->tableWidget->setHorizontalHeaderLabels(header);
    ui->tableWidget->setRowCount(row);    //设置行数

    for(int i = 0; i <= row; i++) {
        HeroRank* temphero=new HeroRank ();
        Student*tempStu = new Student();
        temphero=search<HeroRank>(i,heroRankDatPath);
        tempStu=search<Student>(temphero->userID,studentDatPath);
        QString ID = QString::number(tempStu->ID);
        QString user_id = QString(QString::fromLocal8Bit(tempStu->stu_id, MAX_ID));
        QString name = QString(QString::fromLocal8Bit(tempStu->name, MAX_NAME));
        QString major = QString(QString::fromLocal8Bit(tempStu->major, MAX_MAJOR));
        QString reachTime = fomatTime(temphero->achieveTime);
        ui->tableWidget->setItem(i, 0, new QTableWidgetItem(ID));
        ui->tableWidget->setItem(i, 1, new QTableWidgetItem(user_id));
        ui->tableWidget->setItem(i, 2, new QTableWidgetItem(name));
        ui->tableWidget->setItem(i, 3, new QTableWidgetItem(major));
        ui->tableWidget->setItem(i, 4, new QTableWidgetItem(reachTime));
        delete(tempStu);
        delete (temphero);
    }
    fclose(file);//关闭文件

}

herobank::herobank(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::herobank)
{
    ui->setupUi(this);
    show();

}

herobank::~herobank()
{
    delete ui;
}

void herobank::on_pushButton_clicked()
{
    show();
}
